﻿/// <reference path="../../Core.ts" />

module Supernova {

    export class ShipActionsPanel {

        layout = {
            shipActionsPanel: {
                top: 10, width: 100, right: 10, bottom: 10,
                visible: false,
                controls: {
                    stopButton: {
                        top: 0, height: 40,
                        text: 'Stop',
                        shortcutKey: 'S',
                        help: ['The ship is instructed to stop immediately.'],
                        enabled: false,
                        onClick: this.handleStopButtonClicked,
                        onEnter: this.handleButtonEnter
                    },
                    terraformButton: {
                        top: 50, height: 40,
                        text: 'Terraform',
                        shortcutKey: 'T',
                        help: ['Terraforms the barren planet the ship is currently orbiting.', 'This action consumes terraforming materials present on the ship', 'When the ship runs out of terraforming materials it can no longer terraform barren planets.'],
                        enabled: false,
                        onClick: this.handleTerraformButtonClicked,
                        onEnter: this.handleButtonEnter
                    },
                    coloniseButton: {
                        top: 100, height: 40,
                        text: 'Colonise',
                        shortcutKey: 'C',
                        help: ['Spends one population point to colonise a habitable planet.', 'When the ship runs out of population points it is no longer capable of colonising a planet.'],
                        enabled: false,
                        onClick: this.handleColoniseButtonClicked,
                        onEnter: this.handleButtonEnter
                    },
                    bombardButton: {
                        top: 150, height: 40,
                        text: 'Invade',
                        shortcutKey: 'I',
                        help: ['Instructs the ship to attempt to depopulate an enemy planet.', 'The ship will take damage.', 'If orbitals are present, they will be removed first, and the ship will take increased damage.', 'It is important to assign increased armour or shields to ships capable of invading planets.'],
                        enabled: false,
                        onClick: this.handleBombardButtonClicked,
                        onEnter: this.handleButtonEnter
                    },
                    exploreButton: {
                        top: 200, height: 40,
                        text: 'Explore',
                        shortcutKey: 'N',
                        help: ['Instructs the ship to explore all unexplored star systems automatically.', 'Goodies in newly discovered star systems are collected automatically.'],
                        enabled: false,
                        onClick: this.handleExploreButtonClicked,
                        onEnter: this.handleButtonEnter
                    },
                    repairButton: {
                        top: 250, height: 40,
                        text: 'Repair',
                        shortcutKey: 'R',
                        help: ['The ship stops and repairs itself.', 'The ship is not able to attack while a repair is in progress.', 'To cancel a repair, move the ship.'],
                        enabled: false,
                        onClick: this.handleRepairButtonClicked,
                        onEnter: this.handleButtonEnter
                    },
                    disbandButton: {
                        top: 300, height: 40,
                        text: 'Disband',
                        shortcutKey: '-',
                        help: ['The ship is abandoned.', 'If you have researched Reclaimation then the ship is scrapped and money returned.', 'The amount of money returned is based on the ship\'s remaing armour.'],
                        enabled: false,
                        onClick: this.handleDisbandButtonClicked,
                        onEnter: this.handleButtonEnter
                    }
                }
            }
        }

        public parent: GameUI = null;

        public root: UI.UIControl = null;

        public stopButton: UI.Button = null;
        public terraformButton: UI.Button = null
        public coloniseButton: UI.Button = null
        public bombardButton: UI.Button = null
        public exploreButton: UI.Button = null
        public repairButton: UI.Button = null
        public disbandButton: UI.Button = null

        constructor(parent: GameUI) {
            this.parent = parent;
            this.root = UI.compile(this.layout)[0];
            this.stopButton = UI.getControlByName<UI.Button>(this.root, 'stopButton');
            this.terraformButton = UI.getControlByName<UI.Button>(this.root, 'terraformButton');
            this.coloniseButton = UI.getControlByName<UI.Button>(this.root, 'coloniseButton');
            this.bombardButton = UI.getControlByName<UI.Button>(this.root, 'bombardButton');
            this.exploreButton = UI.getControlByName<UI.Button>(this.root, 'exploreButton');
            this.repairButton = UI.getControlByName<UI.Button>(this.root, 'repairButton');
            this.disbandButton = UI.getControlByName<UI.Button>(this.root, 'disbandButton');
        }

        handleButtonEnter(sender, e) {
            if (!sender.enabled) return;
            var details = [{ type: 'title', text: sender.text + (sender.shortcutKey === undefined ? '' : ' [' + sender.shortcutKey + ']') }];
            for (var i in sender.help) {
                details.push({ type: 'text', text: sender.help[i] });
            }
            toolTipCanvas = ToolTip.render(details);
        }

        handleStopButtonClicked(sender, e) {
            galaxy.stopAction();
        }

        handleColoniseButtonClicked(sender, e) {
            galaxy.coloniseAction(keys.control);
        }

        handleTerraformButtonClicked(sender, e) {
            galaxy.terraformAction(keys.control);
        }

        handleBombardButtonClicked(sender, e) {
            galaxy.bombardAction(keys.control);
        }

        handleExploreButtonClicked(sender, e) {
            galaxy.exploreAction(keys.control);
        }

        handleRepairButtonClicked(sender, e) {
            galaxy.repairAction(keys.control);
        }

        handleDisbandButtonClicked(sender, e) {
            galaxy.disbandAction(keys.control);
        }

    }

}